require(["js/config.js"], function () {
    'use strict';
    require(["axios", "vue", "pace","lodash","iframeResizer","initBootstrap40","mloading","perfect-scrollbar"], 
    function (axios,Vue, pace,_,iFrameResize,initBootstrap,mloading,PerfectScrollbar) {
        let _ps = null;
        //类型   0：模块   1：目录   2：菜单  3：操作
        let vue =new Vue({
            el: '#app',
            data: {
                resource:[],            //资源数据
                moduleId:'',            //选中模块id,
                activeId:'',             //选中菜单
                activeUrl:'',
                topActiveId:'',       //一级菜单选中,
                topOpenId:'',            //一级菜单打开唯一
                onlyTabOpen:true,          //只有一个选项卡
                breadcrumb:'',      //面包屑
                moduleName:''           //面包屑
            },
            mounted: function () {
                this.$nextTick(function () {
                    $mLoadingDiv.mLoading();
                    pace.start({
                        elements: {
                            selectors: ['iframe']
                        }
                    });
                    _ps = new PerfectScrollbar(document.querySelector('.sidebar-nav'),{
                        suppressScrollX:true
                    });
                })
            },
            methods:{
                choiceModule:function(moduleObj){
                    this.moduleId = moduleObj.id;
                    this.moduleName = moduleObj.title;
                },
                //展开一级菜单
                topExpansion:function(nav,moduleId){
                    if(!nav.open){
                        if(this.topOpenId != nav.id){
                            if(this.topOpenId && this.onlyTabOpen){
                                //关闭开着的一级菜单
                                topNavExpansionClosed(this.resource,moduleId,this.topOpenId,this.activeId);
                            }
                            this.topOpenId = nav.id;
                        }
                    }
                    nav.open = !nav.open;
                    _ps.update();
                },
                //展开二级菜单
                expansion:function(nav){
                   nav.open = !nav.open;
                   _ps.update();
                },
                //查看菜单
                viewMenu:function(nav,moduleId,topNav,parentNav){
                    let breadcrumb=`<li class="breadcrumb-item">${this.moduleName}</li>`;
                    if(topNav){
                        //一级目录选中并打开
                        this.topActiveId = topNav.id;
                        if(!topNav.open){
                            topNav.open = true;
                        }
                        otherDirectoryClosed(this.resource,moduleId,topNav.id);
                        breadcrumb+=`<li class="breadcrumb-item">${topNav.title}</li>`;
                        if(parentNav){
                            breadcrumb+=`<li class="breadcrumb-item">${parentNav.title}</li>`;
                        }
                    }else{
                        allOpenNavClosed(this.resource,moduleId);
                        this.topActiveId = '';
                    }

                    breadcrumb+=`<li class="breadcrumb-item active">${nav.title}</li>`;
                    this.breadcrumb = breadcrumb;
                    this.activeId = nav.id;
                    if(this.activeUrl !== nav.href){
                        showModal();
                    }
                    this.activeUrl = nav.href;
                },
                
                //mobile模式
                sidebarMobileToggle:function(){
                    let element = document.getElementsByTagName("body")[0];
                    if(element.className.indexOf("sidebar-mobile-show") === -1){
                        element.className = element.className.concat(" sidebar-mobile-show");
                    }else{
                        element.className = element.className.replace('sidebar-mobile-show','');
                    }
                },
                //侧边栏隐藏显示按钮
                sidebarHideToggle:function(){
                    let element = document.getElementsByTagName("body")[0];
                    if(element.className.indexOf("sidebar-hidden") === -1){
                        element.className = element.className.concat(" sidebar-hidden");
                    }else{
                        element.className = element.className.replace('sidebar-hidden','');
                    }
                    
                },
                sidebarMinimizedToggle:function(){
                    let element = document.getElementsByTagName("body")[0];
                    if(element.className.indexOf("brand-minimized") === -1 && element.className.indexOf("sidebar-minimized") === -1){
                        element.className = element.className.concat(" brand-minimized sidebar-minimized");
                        if(_ps!=null){
                            _ps.destroy();
                            _ps = null;
                        }
                    }else{
                        element.className = element.className.replace('brand-minimized sidebar-minimized','');
                        if(_ps== null){
                            _ps = new PerfectScrollbar(document.querySelector('.sidebar-nav'));
                        }
                    }
                }
            }
        });


        
        

        axios.get("data/data.json")
        .then(function (response) {
            if(response.status === 200 && response.data){
                if(_.isArray(response.data) && response.data.length !=0){
                    //初始化默认的模块
                    Vue.set(vue,"moduleId",response.data[0].id);
                    Vue.set(vue,"moduleName",response.data[0].title);
                    let breadcrumb=`<li class="breadcrumb-item">${response.data[0].title}</li>`;
                    if(_.isArray(response.data[0].children) && response.data[0].children.length !=0){
                        if(response.data[0].children[0].type === 1){
                            breadcrumb+=`<li class="breadcrumb-item active">${response.data[0].children[0].title}</li>`;
                            Vue.set(vue,"activeId",response.data[0].children[0].id);
                            Vue.set(vue,"activeUrl",response.data[0].children[0].href);
                            Vue.set(vue,"breadcrumb",breadcrumb);
                        }else{
                            //再说吧
                        }
                    }
                }
                Vue.set(vue,"resource",response.data);
            }
        })
        .catch(function (error) {
            console.error(error);
        });

        iFrameResize({
            log:false,
            minHeight:document.getElementsByTagName("nav")[0].offsetHeight-75,
            initCallback:function(iframe){
                // console.log('init',iframe);
            },
            resizedCallback: function({iframe,height,width,type}){ // Callback fn when resize is received
                // scrollTo(0, info.iframe.offsetTop);
                // console.log('resize',iframe,height,width,type);
                hideModal();
            },
            messageCallback:function({iframe,message}){
                // console.log('message',iframe,message);
                switch (message) {
                    case 'showModal':
                        showModal();
                        break;
                    case 'hideModal':
                        hideModal();
                        break;
                    default:
                        break;
                }

            },
            scrollCallback:function(x,y){
                console.log('scroll',x,y);
            }
        }, '#mainFrame');


        const $mLoadingDiv = $(".container-fluid");
        function showModal(){
                $mLoadingDiv.mLoading();
                Pace.restart();
        }

        function hideModal(){
            setTimeout(function(){
                $mLoadingDiv.mLoading('hide');
            },700);
            // Pace.stop();
        }


        //关闭开着的一级菜单
        function topNavExpansionClosed(resource,moduleId,menuId,navActiveId){
            let parentNav = false;
            if(_.isArray(resource) && resource.length !== 0){
                for(let i=0;i<resource.length;i++){
                    if(resource[i].id === moduleId){
                        //一级菜单
                        if(_.isArray(resource[i].children) && resource[i].children.length !== 0){
                            for(let j=0;j<resource[i].children.length;j++){
                                if(resource[i].children[j].id===menuId){
                                    resource[i].children[j].open = false;
                                    let children =  resource[i].children[j].children;
                                    //遍历二级目录
                                    if(_.isArray(children) && children.length !== 0 ){
                                        for(let t=0;t<children.length;t++){
                                            if(children[t].type ===2 ){
                                                let chc = children[t].children;
                                                if(_.isArray(chc) && chc.length !== 0){
                                                    for(let n=0;n<chc.length;n++){
                                                        if(chc[n].id === navActiveId){
                                                            parentNav = true;
                                                            break;
                                                        }
                                                    }
                                                }
                                                if(parentNav){
                                                    children[t].open = true;
                                                }else{
                                                    children[t].open = false;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        break;
                    }
                }
            }
        }

        //除指定目录其他都关闭
        function otherDirectoryClosed(resource,moduleId,navId){
            if(_.isArray(resource) && resource.length !== 0){
                for(let i=0;i<resource.length;i++){
                    if(resource[i].id === moduleId){
                        if(_.isArray(resource[i].children) && resource[i].children.length !== 0){
                            //遍历一级菜单
                            for(let j=0;j<resource[i].children.length;j++){
                                if(resource[i].children[j].type ===2 && resource[i].children[j].id !== navId){
                                    resource[i].children[j].open = false;
                                    let children =  resource[i].children[j].children;
                                    //遍历
                                    if(_.isArray(children) && children.length !== 0 ){
                                        for(let t=0;t<children.length;t++){
                                            if(children[t].type ===2 ){
                                                children[t].open = false;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        break;
                    }
                }
            }
        }

        //关闭所有目录
        function allOpenNavClosed(resource,moduleId){
            if(_.isArray(resource) && resource.length !== 0){
                for(let i=0;i<resource.length;i++){
                    if(resource[i].id === moduleId){
                        //一级菜单
                        if(_.isArray(resource[i].children) && resource[i].children.length !== 0){
                            //遍历
                            for(let j=0;j<resource[i].children.length;j++){
                                if(resource[i].children[j].type ===2 ){
                                    resource[i].children[j].open = false;
                                    //二级菜单
                                    let children =  resource[i].children[j].children;
                                    //遍历
                                    if(_.isArray(children) && children.length !== 0 ){
                                        for(let t=0;t<children.length;t++){
                                            if(children[t].type ===2 ){
                                                children[t].open = false;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        break;
                    }
                }
            }
        }

    });
})